package dao.impl;

import dao.ScheduleDao;
import entity.Finance;
import entity.Schedule;
import util.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class ScheduleDaoImpl implements ScheduleDao {
    @Override
    public String insert(String idNumber, String schedule) throws Exception {
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();
        try {
//            执行SQL语句
            String str = "INSERT INTO schedule(idNumber,schedule) VALUES ('" + idNumber +"','"+ schedule +"')";
            stat.executeUpdate(str);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(null,stat,conn);
        }
        return null;
    }

    @Override
    public ArrayList<Schedule> findSchedule(String idNumber) throws Exception {
        ArrayList<Schedule> list = new ArrayList<>();
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();

        ResultSet rs = null;
        try{
            //执行sql语句，并且接收返回的结果集
            String sql = "SELECT * FROM schedule where idNumber = " + "'" + idNumber + "'";
            rs = stat.executeQuery(sql);

            //处理结果集
            while(rs.next()) {
                String sch = rs.getString("schedule");

                //封装Finance对象
                 Schedule schedule= new Schedule(idNumber,sch);

                //将Finance对象保存到集合中
                list.add(schedule);
            }

        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            JDBCUtils.release(rs,stat,conn);
        }
        //将集合对象返回
        return list;
    }
}
